package com.sservice.service.service.impl;

import com.sservice.model.dao.ClienteleAccountMapper;
import com.sservice.model.entity.ClienteleAccount;
import com.sservice.model.vo.ClienteleAccountVo;
import com.sservice.service.service.IClienteleAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * Created by suweiming on 2017/12/20.
 */

@Service
public class ClienteleAccountServiceImpl implements IClienteleAccountService {
    @Autowired
    public ClienteleAccountMapper dao;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return dao.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(ClienteleAccount record) {
        return dao.insert(record);
    }

    @Override
    public int insertSelective(ClienteleAccount record) {
        return dao.insertSelective(record);
    }

    @Override
    public ClienteleAccount selectClientAccountByClientele(String clienteleId) {
        ClienteleAccount ca = dao.selectClientAccountByClientele(clienteleId);
        if (ca == null) {
            ca = new ClienteleAccount();
            ca.setYufuRemain(BigDecimal.valueOf(0));
            ca.setYufuTotal(BigDecimal.valueOf(0));
            ca.setClienteleId(clienteleId);
            int res = dao.insertSelective(ca);
            if (res > 0) {
                return dao.selectClientAccountByClientele(clienteleId);
            } else {
                throw new Error("创建客户余额账户异常");
            }
        }
        return ca;
    }

    @Override
    public ClienteleAccount selectByPrimaryKey(Integer id) {
        return dao.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(ClienteleAccount record) {
        return dao.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(ClienteleAccount record) {
        return dao.updateByPrimaryKey(record);
    }

    @Override
    public int queryByCount(ClienteleAccountVo record) {
        return dao.queryByCount(record);
    }

    @Override
    public List<ClienteleAccountVo> queryByPage(ClienteleAccountVo record) {
        return dao.queryByPage(record);
    }
}
